package com.cy.game

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import com.helloandroid.AppUtil
import com.helloandroid.R
import com.helloandroid.ads.SmallNativeAdUtil
import com.helloandroid.base.BaseDialog
import com.helloandroid.tools.AppConfig
import com.helloandroid.tools.MyLog
import com.helloandroid.tools.MyTimeUtils
import com.helloandroid.tools.NetWorkUtil
import com.helloandroid.vo.Type
import dai.ui.composition.Parallel
import dai.ui.delay
import dai.ui.repeat
import dai.ui.rotateTo
import dai.ui.scaleTo
import dai.ui.single.Delay
import dai.ui.single.RotateTo
import dai.ui.single.ScaleTo
import java.util.*

class WinDialog(ctx: Context,val gameFragment:CyGameFragment) : BaseDialog(ctx) {

    val cyAct = ctx as CyHomeActivity

    // 显示奖励金币
    lateinit var tvCoin: TextView
    lateinit var tvRest: TextView
    lateinit var pbProcess: ProgressBar
    lateinit var ivPassRed: ImageView
    lateinit var progressLayout: RelativeLayout
    //
    lateinit var tvPower:TextView
    lateinit var tvTime:TextView

    var passedLevel = -1

    override fun getContentViewResId(): Int = R.layout.yh_pass_page

    override fun initView() {
        val info = cyAct.mInfo!!
        passedLevel = info.curLevel

        tvCoin = findViewById(R.id.pass_curCoinText)
        tvRest = findViewById(R.id.tvRest)
        pbProcess = findViewById(R.id.pbProgress)
        ivPassRed = findViewById(R.id.pass_red)
        progressLayout = findViewById(R.id.progressLayout)

        tvPower = findViewById(R.id.pass_powerCount)
        tvTime = findViewById(R.id.tvTime)

        // back
        findBtn(R.id.pass_backButton).setOnClickListener {
            // 回到homeActivity
            cancel()
            cyAct.navToCyHome()
        }

        // next
        findBtn(R.id.passButtonNext).setOnClickListener { btnNextClicked() }

        // 查看成语解释
        findBtn(R.id.passButtonCheck).setOnClickListener {
            CyListDialog(gameFragment.board.proverbs, cyAct).show()
        }

        // 显示体力
        findBtn(R.id.powerLayout).setOnClickListener {
            AddPowerDialog(cyAct).show()
        }

        //
        initUI()

        //
        val appViewModel = AppUtil.appViewModel()
        appViewModel.cyPassLevel(passedLevel,AppConfig.CyCoinAwardPerLevel)
        appViewModel.zhuanReportTask(Type.ChengYu)
        info.curLevel += 1

        // 更新 info
        updatePowerInfo(info)
        cyAct.model.startTick {
            updatePowerInfo(info)
        }

        // 广告
        SmallNativeAdUtil.showNativeAd(cyAct,findViewById(R.id.nativeView))
    }

    private fun btnNextClicked(){

        // TODO: 窗口的关闭处理不够优雅.
        // 判断是否是最后一关
        val info = cyAct.mInfo!!
        if (info.curLevel > info.totalLevel) {
            AppUtil.toast("已经通关所有关卡！后续关卡敬请期待！")
            cancel()
            cyAct.navToCyHome()
            return
        }

        // 下一关
        if (info.power < 1){
            // 弹出补充体力窗口
            AddPowerDialog(cyAct).show()
        }else{
            if (!NetWorkUtil.networkConnected()) {
                AppUtil.toast("请打开网络!")
                return
            }
            cancel()
            AppUtil.appViewModel().cyConsumePower()
            info.decreasePower(1)
            cyAct.navToCyGame()
        }
    }

    fun updatePowerInfo(info:CyInfo){
        tvPower.text = "${info.power}"
        if (info.power >= 20){
            tvTime.text = "已满"
        }else{
            tvTime.text = "${MyTimeUtils.format_mmss(CyHomeActivity.TenMinutes - cyAct.countDownTimeMs)}"
        }
    }

    fun initUI() {
        tvCoin.text = "获得${AppConfig.CyCoinAwardPerLevel}金币"

        // 判断是否应该弹出大红包弹窗
        var level = passedLevel
        var red = false
        when {

            // <= 5
            level <= 5 -> {
                tvRest.text = "还有${5 - level}关解锁红包"
                pbProcess.progress = level * 20
                if (level == 5) {
                    red = true
                }
            }

            // <= 10
            level <= 10 -> {
                tvRest.text = "还有${10 - level}关解锁红包"
                pbProcess.progress = (level - 5) * 20
                if (level == 10) {
                    red = true
                }
            }

            // <= 30
            level <= 30 -> {
                tvRest.text = "还有${30 - level}关解锁红包"
                pbProcess.progress = ((level - 10) * 5)
                if (level == 30) {
                    red = true
                }
            }

            //
            else -> {
                if (level <= 50) {
                    tvRest.text = "还有${50 - level}关解锁红包"
                    pbProcess.progress = (level - 30) * 5
                } else {
                    val n = level / 50 + 1
                    tvRest.text = "还有${n * 50 - level}关解锁红包"
                    pbProcess.progress = ((level - (n - 1) * 50) * 2)
                }
                if (level % 50 == 0) {
                    red = true
                }
            }
        }

        // 弹红包对话框...
        if (red) {

            // 延迟一下
            cyAct.model.delayRun(0.1f) {
                showBigRedDialog()
            }

            // 同时来个动画
            ivPassRed.visibility = View.VISIBLE
            progressLayout.visibility = View.INVISIBLE
            playShakeAnim(ivPassRed)
            ivPassRed.setOnClickListener {
              showBigRedDialog()
            }
        } else {
            ivPassRed.visibility = View.INVISIBLE
            progressLayout.visibility = View.VISIBLE
        }
    }

    fun showBigRedDialog(){
        BigRedDialog(passedLevel,cyAct,this::onBigRedClose).show()
    }

    fun onBigRedClose(awardGetted:Boolean){
        if (awardGetted){
            ivPassRed.visibility = View.INVISIBLE
        }
    }

    override fun initData() {
    }

    private fun playShakeAnim(v: View) {
        MyLog.elog("=========playShakeAnim called!")

        val tween1 = ScaleTo(0.25f, 0.8f, 0.8f)
                .scaleTo(0.25f, 1f, 1f)
                .delay(0.5f)

        val tween2 = RotateTo(0.1f, -5f)
                .rotateTo(0.1f, -5f)
                .rotateTo(0.1f, 5f)
                .rotateTo(0.1f, -5f)
                .rotateTo(0.1f, 5f)
                .rotateTo(0.1f, -5f)
                .rotateTo(0.1f, 5f)
                .rotateTo(0.1f, -5f)
                .rotateTo(0.1f, 5f)
                .rotateTo(0.1f, -5f)
                .rotateTo(0.1f, 5f)
                .rotateTo(0.1f, 0f)

        Delay(0.2f)
                .repeat(-1, Parallel(tween1, tween2))
                .runBy(v, gameFragment.tweenModel)
    }

    override fun dismiss() {
        cyAct.model.stopTick()
        super.dismiss()
    }
}